<?php
namespace app\api\controller\damp;
/***
 *      ┌─┐       ┌─┐
 *   ┌──┘ ┴───────┘ ┴──┐
 *   │                 │
 *   │       ───       │
 *   │  ─┬┘       └┬─  │
 *   │                 │
 *   │       ─┴─       │
 *   │                 │
 *   └───┐         ┌───┘
 *       │         │    神兽保佑代码没有bug
 *       │         │
 *       │         │
 *       │         └──────────────┐
 *       │                        │
 *       │                        ├─┐
 *       │                        ┌─┘
 *       │                        │
 *       └─┐  ┐  ┌───────┬──┐  ┌──┘
 *         │ ─┤ ─┤       │ ─┤ ─┤
 *         └──┴──┘       └──┴──┘
 *              营销模块控制器
 */

use think\facade\Db;
use think\Request;
use app\BaseController;
use app\common\controller\ApiController;
use think\App;

class Market extends ApiController
{

    public function initialize()
    {
        parent::initialize();
        $this->market_item = app('app\common\model\market\MarketItem');
        $this->market_item_sku = app('app\common\model\market\MarketItemSku');
        $this->store = app('app\common\model\mall\Store');
        $this->store_address = app('app\common\model\mall\StoreAddress');
        $this->item = app('app\common\model\mall\Item');
        $this->seckill_time = app('app\common\model\market\SeckillTime');
    }

    /**
     * 秒杀列表
     * @param Request $request
     */
    public function seckill_list(Request $request)
    {
        try {
            $params = $request->param();

            if ($params['is_index'] == 1) {
                //获取最近时间段
                $lists = $this->seckill_time->whereTime('start_time' , '>' , date('Y-m-d H:i:s'))
                    ->with(['SeckillItem' => function ($query) {
                        $query->withLimit(2);
                    }])
                    ->order('start_time asc')
                    ->find();
            } else {
                //获取最近时间段
                $lists = $this->seckill_time->whereTime('start_time' , '>' , date('Y-m-d H:i:s'))
                    ->with(['SeckillItem'])
                    ->order('start_time asc')
                    ->select();
            }

            return json(['code' => 200 , 'data' => ['data' => $lists]]);

        } catch ( \Exception $e ) {

            return json(['code' => 202 , 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 秒杀详情/商品详情
     * @param Request $request
     */

    public function seckill_detail(Request $request)
    {
        try {
            $params = $request->param();
            $this->validate($params , [
                'item_id|商品id' => 'require' ,
                'is_market|是否为秒杀商品' => 'require' ,
                //'key|用户登陆态' => 'require' ,
            ]);
            $where['status'] = 1;
            if ($params['is_market'] == 1) {
                $info = $this->market_item->with(['ItemSku' , 'ItemSkuSpec' => function ($query) {
                    $query->with('ItemSpec')->group('spec_id')->select();
                }])->where($where)->find($params['item_id'])->toArray();
            } else {

                $info = $this->item->with(['ItemSku' , 'ItemSkuSpec' => function ($query) {
                    $query->with('ItemSpec')->group('spec_id')->select();
                }])->where($where)->find($params['item_id'])->toArray();

                $info['type'] ==1 && $info['address'] = $this->store_address->where('store_id',$info['store_id'])->select();
            }


            return json(['code' => 200 , 'data' => ['data' => $info]]);

        } catch ( \Exception $e ) {

            return json(['code' => 202 , 'msg' => $e->getMessage()]);
        }
    }
}